package com.mybatisplus.controller;

import com.mybatisplus.pojo.Patent;
import com.mybatisplus.service.PatentService;
import com.mybatisplus.utils.ResultOBJ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/adminapi/patents")
public class PatentController {
    @Autowired
    private PatentService patentService;

    @GetMapping
    public List<Patent> getPatentList(Patent patent) {
        return patentService.getPatentList(patent);
    }

    @PostMapping
    public ResultOBJ addPatent(@RequestBody Patent patent) {
        try {
            patentService.addPatent(patent);
            return ResultOBJ.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.ADD_ERROR;
        }
    }

    @PutMapping(value = "/{id}")
    public ResultOBJ updatePatent(@PathVariable Integer id, @RequestBody Patent patent) {
        try {
            patent.setId(id);
            patentService.updatePatent(patent);
            return ResultOBJ.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.UPDATE_ERROR;
        }
    }

    @DeleteMapping(value = "/{id}")
    public ResultOBJ deletePatent(@PathVariable Integer id) {
        try {
            patentService.deletePatent(id);
            return ResultOBJ.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.DELETE_ERROR;
        }
    }
}


